package com.etoppaas.performance.service.impl;

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.etoppaas.performance.common.AppraisalStatusEnum;
import com.etoppaas.performance.common.PerformanceConstants;
import com.etoppaas.performance.domain.PerAppraisalSpec;
import com.etoppaas.performance.domain.PerTemplateSpec;
import com.etoppaas.performance.mapper.PerAppraisalSpecMapper;
import com.etoppaas.performance.vo.FormulateVo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;
import com.etoppaas.performance.mapper.PerAppraisalInfoMapper;
import com.etoppaas.performance.domain.PerAppraisalInfo;
import com.etoppaas.performance.service.IPerAppraisalInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 *  服务层实现
 * 
 * @author ruoyi
 * @date 2019-06-05
 */
@Service
public class PerAppraisalInfoServiceImpl implements IPerAppraisalInfoService 
{
	@Autowired
	private PerAppraisalInfoMapper perAppraisalInfoMapper;

	@Autowired
	private SysDeptMapper deptMapper;

	@Autowired
	private PerAppraisalSpecMapper appraisalSpecMapper;

	/**
     * 查询信息
     * 
     * @param pkid ID
     * @return 信息
     */
    @Override
	public PerAppraisalInfo selectPerAppraisalInfoById(Long pkid)
	{
	    return perAppraisalInfoMapper.selectPerAppraisalInfoById(pkid);
	}
	
	/**
     * 查询列表
     * 
     * @param perAppraisalInfo 信息
     * @return 集合
     */
	@Override
	public List<PerAppraisalInfo> selectPerAppraisalInfoList(PerAppraisalInfo perAppraisalInfo)
	{
		SysUser user = ShiroUtils.getSysUser();
		perAppraisalInfo.setRoleIds(user.getRoleIds());
		List<SysRole> roles=user.getRoles();
		boolean flag=false;
		for(SysRole role:roles){
			if(role.getRoleKey().equals(PerformanceConstants.performanceLeader)){
				perAppraisalInfo.setIsLeader(PerformanceConstants.performanceLeader);
				flag=true;
				break;
			}else if(role.getRoleKey().equals(PerformanceConstants.performanceStaff)){
				perAppraisalInfo.setIsLeader(PerformanceConstants.performanceStaff);
				flag=true;
				break;
			}
		}
		if(!flag){
			perAppraisalInfo.setIsLeader(PerformanceConstants.performanceStaff);
		}
		perAppraisalInfo.setUserId(user.getUserId());
	    return perAppraisalInfoMapper.selectPerAppraisalInfoList(perAppraisalInfo);
	}
	
    /**
     * 新增
     * 
     * @param perAppraisalInfo 信息
     * @return 结果
     */
	@Override
	public int insertPerAppraisalInfo(PerAppraisalInfo perAppraisalInfo)
	{
	    return perAppraisalInfoMapper.insertPerAppraisalInfo(perAppraisalInfo);
	}
	
	/**
     * 修改
     * 
     * @param perAppraisalInfo 信息
     * @return 结果
     */
	@Override
	public int updatePerAppraisalInfo(PerAppraisalInfo perAppraisalInfo)
	{
	    return perAppraisalInfoMapper.updatePerAppraisalInfo(perAppraisalInfo);
	}

	/**
     * 删除对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deletePerAppraisalInfoByIds(String ids)
	{
		return perAppraisalInfoMapper.deletePerAppraisalInfoByIds(Convert.toStrArray(ids));
	}
	/**
	 * 生成部门员工树
	 * @return
	 */
	@Override
	public List<Ztree> initUserZtree() {

		SysUser user = ShiroUtils.getSysUser();
		Long[] roleIds=user.getRoleIds();
		List<SysUser> sysUsers=deptMapper.selectRoleDeptUsers(roleIds);
		List<Ztree> ztrees = new ArrayList<Ztree>();
		if(CollectionUtils.isEmpty(sysUsers)){
			return ztrees;
		}
		Map<Long,String> depts = new HashMap<>();
		for(SysUser sysUser:sysUsers){
			depts.put(sysUser.getDeptId(),sysUser.getDeptName());
			Ztree ztree = new Ztree();
			ztree.setId(sysUser.getUserId());
			ztree.setName(sysUser.getUserName());
			ztree.setpId(sysUser.getDeptId());
			ztree.setTitle(sysUser.getUserName());
			ztree.setNocheck(false);
			ztree.setOpen(true);
			if(sysUser.getUserId().equals(user.getUserId())){
				ztree.setChecked(false);
			}else {
				ztree.setChecked(true);
			}
			ztrees.add(ztree);
		}
		for(Map.Entry<Long, String> entry : depts.entrySet()){
			Ztree ztree = new Ztree();
			ztree.setId(entry.getKey());
			ztree.setName(entry.getValue());
			ztree.setTitle("部门");
			ztree.setpId(0L);
			ztree.setOpen(true);
			ztrees.add(ztree);
		}
		return ztrees;
	}

	@Override
	public int saveFormulate(FormulateVo vo) {
		String specs=vo.getSpecs();
		List<PerAppraisalSpec> specList=null;
		if(StringUtils.isNotEmpty(specs)){
			specList= JSONObject.parseArray(vo.getSpecs(),PerAppraisalSpec.class);
		}
		if(vo!=null &&vo.getUserIds()!=null && vo.getUserIds().length>0){
			for(Long userId:vo.getUserIds()){
				PerAppraisalInfo  info = new PerAppraisalInfo();
				info.setName(vo.getName());
				info.setStartTime(vo.getStartTime());
				info.setEndTime(vo.getEndTime());
				info.setTemplateId(vo.getTemplateId());
				info.setUserId(userId);
				info.setStatus(vo.getStatus());
				info.setDelFlag("0");
				perAppraisalInfoMapper.insertPerAppraisalInfo(info);
				if(specList!=null&&specList.size()>0){
					for(PerAppraisalSpec specInfo : specList){
						PerAppraisalSpec spec = new PerAppraisalSpec();
						spec.setActualSpecContent(specInfo.getActualSpecContent());
						spec.setActualWeight(specInfo.getActualWeight());
						spec.setAppraisalId(info.getPkid());
						spec.setCreateTime(new Date());
						spec.setSpecId(specInfo.getSpecId());
						appraisalSpecMapper.insertPerAppraisalSpec(spec);
					}
				}

			}

			return vo.getUserIds().length;
		}
		return 0;
	}

    @Override
    public int enabledEva(List<Long> ids) {

		List<PerAppraisalInfo> infos=perAppraisalInfoMapper.selectPerAppraisalInfoListByIds(ids);
		for(PerAppraisalInfo info :infos){
			if(info.getStatus().equals(AppraisalStatusEnum.DISABLED.toString().toLowerCase())){
				PerAppraisalInfo perAppraisalInfo = new PerAppraisalInfo();
				perAppraisalInfo.setPkid(info.getPkid());
				perAppraisalInfo.setStatus(AppraisalStatusEnum.ENABLED.toString().toLowerCase());
				perAppraisalInfoMapper.updatePerAppraisalInfo(perAppraisalInfo);
			}
		}
        return infos.size();
    }

	@Override
	public int saveSelfEvaluation(PerAppraisalInfo infos) {

		String specInfo=infos.getSpecInfos();
		List<PerAppraisalSpec> specList=null;
		if(StringUtils.isNotEmpty(specInfo)){
			specList= JSONObject.parseArray(specInfo,PerAppraisalSpec.class);
		}

		if(specList!=null&&specList.size()>0){
			for(PerAppraisalSpec spec : specList){
				appraisalSpecMapper.updatePerAppraisalSpec(spec);
			}
		}
		return perAppraisalInfoMapper.updatePerAppraisalInfo(infos);
	}
}
